home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / www / src / WWW / Library / Implementation / HTFWriter.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-21  |  4.6 KB  |  227 lines

  1. /*        FILE WRITER                HTFWrite.h
  2. **        ===========
  3. **
  4. **    This version of the stream object just writes to a C file.
  5. **    The file is assumed open and left open.
  6. **
  7. **    Bugs:
  8. **        strings written must be less than buffer size.
  9. */
  10.  
  11. #include "HTFWriter.h"
  12.  
  13. #include "HTFormat.h"
  14. #include "HTAlert.h"
  15. #include "HTFile.h"
  16.  
  17. /*        Stream Object
  18. **        ------------
  19. */
  20.  
  21. struct _HTStream {
  22.     CONST HTStreamClass *    isa;
  23.     
  24.     FILE *            fp;
  25.     char *             end_command;
  26.     char *             remove_command;
  27. };
  28.  
  29.  
  30. /*_________________________________________________________________________
  31. **
  32. **            A C T I O N     R O U T I N E S
  33. **  Bug:
  34. **    All errors are ignored.
  35. */
  36.  
  37. /*    Character handling
  38. **    ------------------
  39. */
  40.  
  41. PRIVATE void HTFWriter_put_character ARGS2(HTStream *, me, char, c)
  42. {
  43.     putc(c, me->fp);
  44. }
  45.  
  46.  
  47.  
  48. /*    String handling
  49. **    ---------------
  50. **
  51. **    Strings must be smaller than this buffer size.
  52. */
  53. PRIVATE void HTFWriter_put_string ARGS2(HTStream *, me, CONST char*, s)
  54. {
  55.     fputs(s, me->fp);
  56. }
  57.  
  58.  
  59. /*    Buffer write.  Buffers can (and should!) be big.
  60. **    ------------
  61. */
  62. PRIVATE void HTFWriter_write ARGS3(HTStream *, me, CONST char*, s, int, l)
  63. {
  64.     fwrite(s, 1, l, me->fp); 
  65. }
  66.  
  67.  
  68.  
  69.  
  70. /*    Free an HTML object
  71. **    -------------------
  72. **
  73. **    Note that the SGML parsing context is freed, but the created
  74. **    object is not,
  75. **    as it takes on an existence of its own unless explicitly freed.
  76. */
  77. PRIVATE void HTFWriter_free ARGS1(HTStream *, me)
  78. {
  79.     fflush(me->fp);
  80.     if (me->end_command) {        /* Temp file */
  81.         fclose(me->fp);
  82.         HTProgress(me->end_command);    /* Tell user what's happening */
  83.     system(me->end_command);
  84.     free (me->end_command);
  85.     if (me->remove_command) {
  86.         system(me->remove_command);
  87.         free(me->remove_command);
  88.     }
  89.     }
  90.  
  91.     free(me);
  92. }
  93.  
  94. /*    End writing
  95. */
  96.  
  97. PRIVATE void HTFWriter_end_document ARGS1(HTStream *, me)
  98. {
  99.     fflush(me->fp);
  100. }
  101.  
  102.  
  103.  
  104. /*    Structured Object Class
  105. **    -----------------------
  106. */
  107. PRIVATE CONST HTStreamClass HTFWriter = /* As opposed to print etc */
  108. {        
  109.     "FileWriter",
  110.     HTFWriter_free,
  111.     HTFWriter_end_document,
  112.     HTFWriter_put_character,     HTFWriter_put_string,
  113.     HTFWriter_write
  114. }; 
  115.  
  116.  
  117. /*    Subclass-specific Methods
  118. **    -------------------------
  119. */
  120.  
  121. PUBLIC HTStream* HTFWriter_new ARGS1(FILE *, fp)
  122. {
  123.     HTStream* me;
  124.     
  125.     if (!fp) return NULL;
  126.  
  127.     me = (HTStream*)malloc(sizeof(*me));
  128.     if (me == NULL) outofmem(__FILE__, "HTML_new");
  129.     me->isa = &HTFWriter;       
  130.  
  131.     me->fp = fp;
  132.     me->end_command = NULL;
  133.     me->remove_command = NULL;
  134.  
  135.     return me;
  136. }
  137.  
  138. /*    Make system command from template
  139. **    ---------------------------------
  140. **
  141. **    See mailcap spec for description of template.
  142. */
  143. /* @@ to be written.  sprintfs will do for now.  */
  144.  
  145.  
  146. /*    Take action using a system command
  147. **    ----------------------------------
  148. **
  149. **    originally from Ghostview handling by Marc Andreseen.
  150. **    Creates temporary file, writes to it, executes system command
  151. **    on end-document.  The suffix of the temp file can be given
  152. **    in case the application is fussy, or so that a generic opener can
  153. **    be used.
  154. */
  155. PUBLIC HTStream* HTSaveAndExecute ARGS3(
  156.     HTPresentation *,    pres,
  157.     HTParentAnchor *,    anchor,    /* Not used */
  158.     HTStream *,        sink)    /* Not used */
  159.  
  160. #ifdef unix
  161. #define REMOVE_COMMAND "/bin/rm -f %s\n"
  162. #endif
  163. #ifdef VMS
  164. #define REMOVE_COMMAND "delete/noconfirm/nolog %s.."
  165. #endif
  166.  
  167. #ifdef REMOVE_COMMAND
  168. {
  169.     char *fnam;
  170.     CONST char * suffix;
  171.     
  172.     HTStream* me;
  173.     
  174.     me = (HTStream*)malloc(sizeof(*me));
  175.     if (me == NULL) outofmem(__FILE__, "Save and execute");
  176.     me->isa = &HTFWriter;  
  177.     
  178.     /* Save the file under a suitably suffixed name */
  179.     
  180.     suffix = HTFileSuffix(pres->rep);
  181.  
  182.     fnam = (char *)malloc (L_tmpnam + 16 + strlen(suffix));
  183.     tmpnam (fnam);
  184.     if (suffix) strcat(fnam, suffix);
  185.     
  186.     me->fp = fopen (fnam, "w");
  187.     if (!me->fp) {
  188.     HTAlert("Can't open temporary file!");
  189.         free(fnam);
  190.     free(me);
  191.     return NULL;
  192.     }
  193.  
  194. /*    Make command to process file
  195. */
  196.     me->end_command = (char *)malloc (
  197.                 (strlen (pres->command) + 10+ 3*strlen(fnam))
  198.                  * sizeof (char));
  199.     if (me == NULL) outofmem(__FILE__, "SaveAndExecute");
  200.     
  201.     sprintf (me->end_command, pres->command, fnam, fnam, fnam);
  202.  
  203.     me->remove_command = NULL;    /* If needed, put into end_command */
  204. #ifdef NOPE
  205. /*    Make command to delete file
  206. */ 
  207.     me->remove_command = (char *)malloc (
  208.                 (strlen (REMOVE_COMMAND) + 10+ strlen(fnam))
  209.                  * sizeof (char));
  210.     if (me == NULL) outofmem(__FILE__, "SaveAndExecute");
  211.     
  212.     sprintf (me->remove_command, REMOVE_COMMAND, fnam);
  213. #endif
  214.  
  215.     free (fnam);
  216.     return me;
  217. }
  218.  
  219. #else    /* can do remove */
  220. { return NULL; }
  221. #endif
  222.  
  223.  
  224. /*    Format Converter using system command
  225. **    -------------------------------------
  226. */
  227.